/**
 * Created by Demon on 2015/6/15.
 */
var CleanPage = function () {

    var inputs = {
        E227: $("#E227"),
        E228: $("#E228")
    };

    var selects = {
        E227: $("#E227_select"),
        E228: $("#E228_select")
    };

    function selectHandle() {
        selects.E227.on("change", function () {
            inputs.E227.val($(this).val());
        });

        inputs.E227.val(selects.E227.children("option:first").val());

        selects.E228.on("change", function () {
            inputs.E228.val($(this).val());
        });

        inputs.E228.val(selects.E228.children("option:first").val());
    }

    function calculateInit() {
        $.ajax({
            url: basePath + "combustion/clean/getCalculateInit",
            dataType: "json",
            type: "post"
        })
    }

    return {
        init: function () {
            //selectHandle();
            calculateInit();
        },
        updata: function (ExParam) {
            var save = function (exParam, exVal) {
                if (validate.isNotNumber(exVal)) {
                    validate.invalid();
                    return;
                }
                $.ajax({
                    url: basePath + "combustion/clean/saveValueOfCleanEx",
                    dataType: "json",
                    type: "post",
                    data: exParam + "=" + exVal + "&paramName=" + exParam + "&paramValue=" + exVal
                });
                validate.valid();
            };
            var $obj = $("#" + ExParam);
            var ExVal = $obj.val().trim();
            validate.init($obj);
            if (ExVal != "") {
                if ($obj.hasClass("nofill"))
                    return;
                save(ExParam, ExVal);
            } else {
                if ($obj.hasClass("calculable")) {
                    $.ajax({
                        url: basePath + "admin/formal/calculate",
                        dataType: "json",
                        type: "post",
                        //ȼ��ǰ׺ R
                        data: "paramName=R" + ExParam,
                        success: function (data) {
                            save(ExParam, data);
                            switch (ExParam) {
                                case "E255":
                                    $obj.val(data > 0 ? suan : jian);
                                    break;
                                case "E331":
                                    $obj.val(data < 1.5 ? feng : ban);
                                    break;
                                default :
                                    $obj.val(data);
                            }
                        }
                    });
                }
                else
                    validate.invalid();
            }
        },
        calcul:function (ExParam) {
            Metronic.blockUI({
                boxed:true,
                message:"数据计算中..."
            });
            var save = function (exParam, exVal) {
                if (validate.isNotNumber(exVal)) {
                    validate.invalid();
                    return;
                }
                $.ajax({
                    url: basePath + "combustion/clean/saveValueOfCleanEx",
                    dataType: "json",
                    type: "post",
                    data: exParam + "=" + exVal + "&paramName=" + exParam + "&paramValue=" + exVal
                });
                validate.valid();
            };

                var $obj = $("#" + ExParam);
                $.ajax({
                    url: basePath + "admin/formal/calculate",
                    dataType: "json",
                    type: "post",
                    data: "paramName=R" + ExParam,
                    success: function (data) {
                        Metronic.unblockUI();
                        save(ExParam, data);
                        switch (ExParam) {
                            case "E255":
                                $obj.val(data > 0 ? suan : jian);
                                break;
                            case "E331":
                                $obj.val(data < 1.5 ? feng : ban);
                                break;
                            default :
                                $obj.val((data == 0 || data == undefined) ? "数据填写不完整无法计算" : data);
                        }
                    }
                });
        }
    }
}();